<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <textarea id="log" rows="30" cols="30"></textarea>
    <script src="utils.js"></script>
    <script>
        window.onload = function () {
            var canvas = document.getElementById('canvas'),
                context = canvas.getContext('2d'),
                log = document.getElementById('log'),
                mouse = utils.captureMouse(canvas),
                rect = { x: canvas.width / 2, y: canvas.height / 2 }

            function drawFrame() {
                window.requestAnimationFrame(drawFrame, context)
                context.clearRect(0, 0, canvas.width, canvas.height)

                var dx = mouse.x - rect.x,
                    dy = mouse.y - rect.y,
                    dist = Math.sqrt(dx * dx + dy * dy)
                context.fillStyle = '#00ff00'
                context.fillRect(rect.x -2, rect.y-2, 4,4)

                context.beginPath()
                context.moveTo(rect.x, rect.y)
                context.lineTo(mouse.x, mouse.y)
                context.closePath()
                context.stroke()


                log.value = 'mouse to rect distance: '+ dist
            }
            drawFrame()


        }
    </script>
</body>

</html>